package com.example.baseproject.login;

import android.content.Intent;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.example.baseproject.R;
import com.example.baseproject.databinding.ActivityLoginBinding;
import com.tjcd.project.library_base.base.activity.BaseNetActivity;
import com.tjcd.project.library_base.base.model.BaseViewModel;
import com.tjcd.project.library_base.base.network.SSLSocketClient;
import com.tjcd.project.library_base.base.rsaUtils.binary.Base64;

import java.io.IOException;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.X509TrustManager;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/11/18
 * @Description
 * @SpittingGrooves
 */
public class LoginActivity extends BaseNetActivity<ActivityLoginBinding> {

    private void networkRequest() {
        try {
            Long timestamp = System.currentTimeMillis();
            String accessSecret = "414f3961bb0e5ec7368213d232b625ff355c8f68b768852523db73fdb77604e4";

            String stringToSign = timestamp + "\n" + accessSecret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
            LogUtils.e(sign);
            ThreadUtils.executeBySingle(new ThreadUtils.Task<Object>() {
                @Override
                public Object doInBackground() throws Throwable {
                    RequestBody formBody = RequestBody.create(MediaType.parse("application/json"), "{\"file_link\":\"https://zh-jx.oss-cn-beijing.aliyuncs.com/oa/dev/hardware/11.JPEG \"}");
                    Request request = new Request.Builder()
                            .url("http://api.open.pengtu.net/ai/steel_pipe")
                            .addHeader("Content-Type", "application/json")
                            .addHeader("Access-Key", "bd0ee726c82a1418a5c8786fe6811a5e")
                            .addHeader("Timestamp", String.valueOf(timestamp))
                            .addHeader("Signature", sign)
                            .post(formBody)
                            .build();
                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            .readTimeout(1000 * 15, TimeUnit.MILLISECONDS) // 设置读取超时为60秒
                            .connectTimeout(1000 * 15, TimeUnit.MILLISECONDS) // 设置链接超时为60秒
                            .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), new X509TrustManager() {
                                @Override
                                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                                }

                                @Override
                                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                                }

                                @Override
                                public X509Certificate[] getAcceptedIssuers() {
                                    return new X509Certificate[0];
                                }
                            }) // 不验证SSL证书
                            .hostnameVerifier(SSLSocketClient.getHostnameVerifier()) // 取消验证
                            .build();

                    Call mCall = okHttpClient.newCall(request);
                    mCall.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            LogUtils.e("失败 " + "\n错误信息 = " + e.toString());
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            LogUtils.e("成功 code = " + response.code());
                            LogUtils.e("成功 body = " + response.body().string());
                        }
                    });
                    return null;
                }

                @Override
                public void onSuccess(Object result) {

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onFail(Throwable t) {

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected Class<BaseViewModel> setViewModelClass() {
        return null;
    }

    @Override
    protected void init() {
        vBinding.loginIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                networkRequest();
                Intent intent = new Intent();
                intent.putExtra("key","返回值");
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_login;
    }

}